package cn.hncu.booksManagementSystem.book.ui;

import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;

import cn.hncu.booksManagementSystem.book.business.ebi.BookEbi;
import cn.hncu.booksManagementSystem.book.business.factory.BookEbiFactory;
import cn.hncu.booksManagementSystem.book.vo.BookModel;
import cn.hncu.booksManagementSystem.common.enums.XxxIdEnum;
import cn.hncu.booksManagementSystem.utils.GeneratorXxxID_Util;

public class AddPanel extends JPanel {
	
	private static final long serialVersionUID = 1L;
	private JFrame mainFrame;
	private JTextField tfdID;
	private JTextField tfdName;
	private JTextField tfdIn;
	private JTextField tfdOut;
    private JTextField publish;
	
	//注入ebi
	private BookEbi ebi = BookEbiFactory.getInstanceOfBookEbi();
	
	/**
	 * Create the panel.
	 */
	public AddPanel(JFrame mainFrame) {
		this.mainFrame = mainFrame;
		initContentPane();
	}
	
	public void initContentPane() {
		setLayout(null);
		
		JLabel label = new JLabel("添加书籍");
		label.setHorizontalAlignment(SwingConstants.CENTER);
		label.setForeground(Color.RED);
		label.setFont(new Font("微软雅黑", Font.BOLD, 36));
		label.setBounds(312, 44, 158, 43);
		add(label);
		
		JLabel lblid = new JLabel("编号：");
		lblid.setForeground(Color.BLUE);
		lblid.setFont(new Font("微软雅黑", Font.BOLD, 24));
		lblid.setHorizontalAlignment(SwingConstants.CENTER);
		lblid.setBounds(91, 137, 86, 32);
		add(lblid);
		
		tfdID = new JTextField();
		tfdID.setFont(new Font("微软雅黑", Font.PLAIN, 16));
		tfdID.setBounds(184, 141, 204, 32);
//		tfdID.setEnabled(false);        // 设置为不可编辑
		add(tfdID);
		tfdID.setColumns(10);
		
		JLabel label_1 = new JLabel("名称：");
		label_1.setHorizontalAlignment(SwingConstants.CENTER);
		label_1.setForeground(Color.BLUE);
		label_1.setFont(new Font("微软雅黑", Font.BOLD, 24));
		label_1.setBounds(398, 137, 136, 32);
		add(label_1);
		
		tfdName = new JTextField();
		tfdName.setFont(new Font("微软雅黑", Font.PLAIN, 16));
		tfdName.setColumns(10);
		tfdName.setBounds(514, 141, 188, 32);
		add(tfdName);
		
		JLabel label_2 = new JLabel("售价：");
		label_2.setHorizontalAlignment(SwingConstants.CENTER);
		label_2.setForeground(Color.BLUE);
		label_2.setFont(new Font("微软雅黑", Font.BOLD, 24));
		label_2.setBounds(398, 255, 136, 32);
		add(label_2);

		
		JButton btnAdd = new JButton("添加");
		btnAdd.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				//1收集参数 id,name,inPrice,outPrice
				String id = tfdID.getText();
				String name = tfdName.getText();
				String strInPrice = tfdIn.getText();
				String strOutPrice = tfdOut.getText();
				double inPrice = 0.0;
				double outPrice = 0.0;
				if (name==null || name.trim().length()==0) {
					JOptionPane.showMessageDialog(null, "图书名不能为空！");
					return;
				}
				
				if (strInPrice==null||strInPrice.trim().length()==0) {
					JOptionPane.showMessageDialog(null, "图书进价不能为空！");
					return;
				}
				
				if (strOutPrice==null || strOutPrice.trim().length()==0) {
					JOptionPane.showMessageDialog(null, "图书售价不能为空！");
					return;
				}
				try {
					inPrice = Double.parseDouble(strInPrice.trim());
				} catch (NumberFormatException e1) {
					JOptionPane.showMessageDialog(null, "图书进价格式错误，只能是浮点型");
				}
				try {
					outPrice = Double.parseDouble(strOutPrice.trim());
				} catch (NumberFormatException e1) {
					JOptionPane.showMessageDialog(null, "图书售价格式错误，只能是浮点型");
				}
				
				//2 组织参数
				BookModel book = new BookModel();
				book.setId(id);
				book.setName(name);
				book.setInPrice(inPrice);
				book.setOutPrice(outPrice);
				
				//3 调用逻辑层
				boolean boo = ebi.add(book);
				
				//4 返回结果界面
				if (boo) {
					mainFrame.setContentPane(new ListPanel(mainFrame));
					mainFrame.validate();
				}else {
					JOptionPane.showMessageDialog(null, "图书已存在，请重新输入");
				}
			}
		});
		btnAdd.setForeground(Color.BLUE);
		btnAdd.setFont(new Font("微软雅黑", Font.BOLD, 24));
		btnAdd.setBounds(203, 358, 147, 43);
		add(btnAdd);
		
		JButton btnBack = new JButton("返回");
		btnBack.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				mainFrame.setContentPane(new ListPanel(mainFrame));
				mainFrame.validate();
			}
		});
		btnBack.setForeground(Color.BLUE);
		btnBack.setFont(new Font("微软雅黑", Font.BOLD, 24));
		btnBack.setBounds(460, 358, 147, 43);
		add(btnBack);
		
		JLabel label_3 = new JLabel("进价：");
		label_3.setHorizontalAlignment(SwingConstants.CENTER);
		label_3.setForeground(Color.BLUE);
		label_3.setFont(new Font("微软雅黑", Font.BOLD, 24));
		label_3.setBounds(68, 255, 136, 32);
		add(label_3);
		
		tfdIn = new JTextField();
		tfdIn.setFont(new Font("微软雅黑", Font.PLAIN, 16));
		tfdIn.setColumns(10);
		tfdIn.setBounds(184, 255, 188, 32);
		add(tfdIn);
		
		tfdOut = new JTextField();
		tfdOut.setFont(new Font("微软雅黑", Font.PLAIN, 16));
		tfdOut.setColumns(10);
		tfdOut.setBounds(514, 255, 188, 32);
		add(tfdOut);

        JLabel label_4 = new JLabel("出版社：");
        label_4.setHorizontalAlignment(SwingConstants.CENTER);
        label_4.setForeground(Color.BLUE);
        label_4.setFont(new Font("微软雅黑", Font.BOLD, 24));
        label_4.setBounds(68, 255, 136, 32);
        add(label_4);

        publish = new JTextField();
        tfdIn.setFont(new Font("微软雅黑", Font.PLAIN, 16));
        tfdIn.setColumns(10);
        tfdIn.setBounds(184, 255, 188, 32);
        add(tfdIn);
	}
}
